package zmee.bid.oa;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.workflow.api.AgentExecution;
import kd.bos.workflow.engine.extitf.IWorkflowPlugin;
import zmee.entity.FrontWorkflowRequestInfoin0;
import zmee.util.OARequestUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author lijiawang
* @Date 2022/12/10 9:16
* @Description:项目立项发文
*/
public class PmasProApprovalOA implements IWorkflowPlugin {
    private Log log= LogFactory.getLog(SrcBiddocOperPlugin.class);
    @Override
    public void notify(AgentExecution execution) {
        DynamicObject single = BusinessDataServiceHelper.loadSingle(execution.getBusinessKey(), execution.getEntityNumber());
        Map info=new HashMap();
        info.put("resource","优采云");
        DynamicObject submitter = single.getDynamicObject("creator");
        DynamicObject user = BusinessDataServiceHelper.loadSingle(submitter.getPkValue(), "bos_user");
        info.put("usderid",user.getString("username"));
        info.put("accountType",1);
        info.put("name",single.getString("billname")+"【立项请示】");//请求标题
        info.put("workflowName","环能实业-发文审核流程");//工作流标题
        info.put("workflowId","5392");//工作流ID
        //info.put("isnextflow",0);
        //表头信息
        Map<String, Object> mainfieldMap = OAutil.getMainfieldMap(single);
        //立项发文附件
        List<Map<String, Object>> attachmentpanels = AttachmentServiceHelper.getAttachments(single.getDynamicObjectType().getName(), single.getPkValue(), "zmee_attachmentpanelap");
        List<Map<String,String>> stringListMap = OAutil.urlList(attachmentpanels,"zw");
        //分录附件
        List<Map<String,String>> datafieldMap = OAutil.getDatafieldMap(single);
        for (Map<String,String> fieldMap:datafieldMap) {
            stringListMap.add(fieldMap);
        }
        //单据数据转请求数据
        FrontWorkflowRequestInfoin0 requestInfoin0= OARequestUtil.getFrontWorkflowRequestInfoin0(info,mainfieldMap,stringListMap);
        log.info("==发文-requestInfoin0=="+requestInfoin0);
        //请求参数
        String body = OARequestUtil.workflowRequestBody(requestInfoin0);
        log.info("==发文-body=="+body);
        //调用OA审批接口
        String request = OARequestUtil.doCreateWorkflowRequest(body);
        log.info("==发文-request=="+request);
    }

}
